package com.liu.stockmarket.gson

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import com.liu.stockmarket.utils.YMDHM
import com.liu.stockmarket.utils.YMDHMS
import com.liu.stockmarket.utils.YMDHMSParse
import java.time.LocalDateTime
import com.google.gson.stream.JsonReader as JsonReader1

class LocalDateTimeAdapter : TypeAdapter<LocalDateTime>() {
    override fun write(out: JsonWriter?, value: LocalDateTime?) {
        out?.let {
            if (value == null) {
                it.nullValue()
            } else {
                it.value(value.YMDHMS())
            }
        }
    }

    override fun read(`in`: JsonReader1?): LocalDateTime? {
        return `in`?.let {
            if (it.peek() == JsonToken.NULL) {
                it.nextNull()
                return null
            }
            it.nextString().YMDHMSParse()
        }

    }
}